package shangpin

import (
	"server/aasd/lysd/shangpinsku"
	"server/amtf"
	"server/global"
	"server/model/common/response"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/datatypes"
)

func A成组件() (组件 amtf.S组件) {
	组件 = amtf.S组件{
		Name:   "商品",
		A英文名:   "shangpin",
		Amodel: Smodel{},
		A其他api: &S其他api{},
	}
	return
}

type Smodel struct {
	amtf.S基础model
	A名称   *string              `json:"名称" gorm:"column:名称;comment:;"`
	A类别   *int                 `json:"类别" gorm:"column:类别;comment:;"`
	A图片   datatypes.JSON       `json:"图片" gorm:"column:图片;comment:;"`
	A关键词  datatypes.JSON       `json:"关键词" gorm:"column:关键词;comment:;type:text;"`
	A价格   *int                 `json:"价格" gorm:"column:价格;comment:;"`
	A详细说明 *string              `json:"详细说明" gorm:"column:详细说明;comment:;type:text;"`
	SKUS  []shangpinsku.Smodel `json:"SKUS" gorm:"foreignKey:A商品id;references:ID"`
	A可选属性 datatypes.JSON       `json:"可选属性" gorm:"column:可选属性; comment:;type:text;"`
}

func (Smodel) TableName() string {
	return "lysd_商品"
}

type S其他api struct {
}

func (a *S其他api) A取选项集(c *gin.Context) {
	dataSource, err := A取选项集()
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(dataSource, c)
}

func A取选项集() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	父级id := make([]map[string]any, 0)

	global.GVA_DB.Table("lysd_商品").Where("deleted_at IS NULL").Select("类别 as label,id as value").Scan(&父级id)
	res["父级id"] = 父级id
	return
}
